<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构部分卡片外框 -->
      <el-card class="box-card">
        <!-- 头部使用标签页来构建 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- layout布局组织架构 -->
            <el-row class="department-header">
              <!-- 左侧布局 -->
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <!-- 右侧 -->
              <el-col :span="4">
                <!-- 再次分为两个部分 -->
                <el-row type="flex">
                  <el-col>负责人</el-col>
                  <el-col>
                    <!--  下拉菜单组件 -->
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDeptFn('')">
                          添加子部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 在此处渲染树形图 -->
            <el-tree :data="treeData" :props="defaultProps" default-expand-all>
              <!-- 树形图内部数据 -->
              <template v-slot="{ data }">
                <el-row type="flex" style="width: 100%" justify="space-between">
                  <!-- 左侧布局 -->
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <!-- 右侧 -->
                  <el-col :span="4">
                    <!-- 再次分为两个部分 -->
                    <el-row type="flex">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <!--  下拉菜单组件 -->
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                              @click.native="addDeptFn(data.id)"
                            >
                              添加子部门
                            </el-dropdown-item>
                            <el-dropdown-item @click.native="editDept(data.id)">
                              编辑部门
                            </el-dropdown-item>
                            <!-- 当有data且data里有children这一项时,才能显示删除按钮,否则不允许删除 -->
                            <el-dropdown-item
                              v-if="data && !data.children"
                              @click.native="delDept(data.id)"
                            >
                              删除部门
                            </el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <!-- 弹层组件 -->
        <!-- 关闭方式有四种,一般需要禁用遮罩层,与esc -->
        <el-dialog
          :title="isEdit ? '编辑部门' : '新增部门'"
          :visible.sync="dialogVisible"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!-- 弹出层组件 -->
          <!-- 显示数据不刷新原因,子组件未销毁,当再次点击时,不再执行created -->
          <deptdialog
            v-if="dialogVisible"
            :depts-id="deptsId"
            :is-edit="isEdit"
            :validlist="validList"
            @addevent="addFn"
            @gooutevent="goOutFn"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { delDeptApi, getDepartmentListApi } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import deptdialog from '@/views/departments/deptDialog.vue'
export default {
  components: {
    deptdialog
  },
  data() {
    return {
      activeName: 'first',
      // 树形控件数据
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dialogVisible: false,
      deptsId: '',
      isEdit: false,
      validList: []
    }
  },
  created() {
    // 调用获取组织架构函数
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      // 调用获取组织架构接口
      const res = await getDepartmentListApi()
      // 精简需要做重复验证的数据
      this.validList = res.data.depts.map((item) => ({
        name: item.name,
        code: item.code,
        id: item.id,
        pid: item.pid
      }))
      this.treeData = tranListToTreeData(res.data.depts)
    },

    // 点击增加按钮出现弹出层
    addDeptFn(val) {
      this.dialogVisible = true
      this.deptsId = val
      this.isEdit = false
    },
    // 编辑按钮
    editDept(val) {
      // 打开弹框
      this.dialogVisible = true
      // 传入id
      this.deptsId = val
      // 改变编辑状态
      this.isEdit = true
    },
    // 删除按钮
    async delDept(id) {
      // 弹出询问框
      const isdel = await this.$confirm(
        '此操作将永久删除部门, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (isdel === 'cancel') return this.$message('您已取消删除')
      const res = await delDeptApi(id)
      this.$message.success(res.message)
      // 重新渲染
      this.getDepartmentList()
    },
    addFn(val) {
      // 关闭弹窗
      this.dialogVisible = false
      // 更新数据
      this.getDepartmentList()
      this.$message.success(val)
    },
    goOutFn() {
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>
